﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Modeling_Sub_Pages_NewsPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WelcomeUser"] != null)
        {
            if (Session["EmployeeID"] != null && Session["Role"].ToString().Equals("EAS"))
            {
            }
            else
            {
                Response.Redirect("~/LoginMessage.aspx?id=1");
            }
        }
        else
        {
            Response.Redirect("~/Login.aspx?id=2");
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Calendar1.Visible = true;
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        DateTime date = Calendar1.SelectedDate;
        txtDateTime.Text = date.ToString("dd-MM-yyyy");
        Calendar1.Visible = false;
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtTitle.Text = "";
        txtDateTime.Text = "";
        txtLocation.Text = "";
        txtContent.Text = "";
        txtHour.Text = "";
        txtMinute.Text = "";
        txtHourEnd.Text = "";
        txtMinuteEnd.Text = "";
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();
        
        string title = txtTitle.Text;
        string location = txtLocation.Text;
        string content = txtContent.Text;

        int hour = Convert.ToInt32(txtHour.Text);
        int minute = Convert.ToInt32(txtMinute.Text);

        DateTime datestart = new DateTime(Calendar1.SelectedDate.Year, Calendar1.SelectedDate.Month, Calendar1.SelectedDate.Day, hour, minute, 0);

        string filePath = "~/Image/noimage.jpg";

        int hourEnd = Convert.ToInt32(txtHourEnd.Text);
        int minuteEnd = Convert.ToInt32(txtMinuteEnd.Text);

        DateTime dateEnd = new DateTime(Calendar1.SelectedDate.Year, Calendar1.SelectedDate.Month, Calendar1.SelectedDate.Day, hourEnd, minuteEnd, 0);

        // Đoạn lệnh này kiểm tra có file trong fileUpload ko
        if (fuImage.HasFile)
        {
            CheckValid cv = new CheckValid();

            // kiểm tra xem file up lên có phải dạng ảnh ko(tránh virus)
            if (cv.CheckFileType(fuImage.FileName))
            {
                filePath = "~/Image/" + fuImage.FileName;

                // kiểm tra xem có tồn tại đường dẫn file này trên server ko
                if (System.IO.File.Exists(MapPath(filePath)))
                {
                    int counter = 2;

                    string tempFileName = "";

                    while (System.IO.File.Exists(MapPath(filePath)))
                    {
                        tempFileName = counter.ToString() + fuImage.FileName;

                        filePath = "~/Image/" + tempFileName;

                        counter++;
                    }
                }

                fuImage.SaveAs(MapPath(filePath));
            }
        }

        CheckValid cvl = new CheckValid();

        if (cvl.checkDate(datestart) == 1)
        {
            if (hourEnd > hour)
            {
                // Vào MaintainMessage.aspx để xem thông báo
                if (!udm.addNewEvent(udm.createConnection(), title, datestart,dateEnd, location, filePath, content))
                {
                    Response.Redirect("MaintainMessage.aspx?ID=1");
                }
                else
                {
                    Response.Redirect("MaintainMessage.aspx?ID=0");
                }
            }
        }     
    }
    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        CheckValid cv = new CheckValid();

        int hour = Convert.ToInt32(txtHour.Text);
        int minute = Convert.ToInt32(txtMinute.Text);

        DateTime datestart = new DateTime(Calendar1.SelectedDate.Year, Calendar1.SelectedDate.Month, Calendar1.SelectedDate.Day, hour, minute, 0);

        if (cv.checkDate(datestart) == 1)
        {
            args.IsValid = true;
        }
        else {
            args.IsValid = false;
        }

    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        int hourStart = Convert.ToInt32(txtHour.Text);
        int hourEnd = Convert.ToInt32(txtHourEnd.Text);

        if (hourEnd > hourStart)
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }
}